package tasks;

import java.io.IOException;

import tools.Parametres;
import tools.Utils;
import core.Peer;

public class SendHandshake implements Runnable {
	private Peer peer;
	private byte[] infoHash;
	
	public SendHandshake(Peer peer, byte[] infoHash) {
		this.peer = peer;
		this.infoHash = infoHash;
	}
	
    @Override
    public void run() {
        byte[] length_protocol = new byte[]{19};
        byte[] reserve = new byte[8];
        byte[] my_id = Parametres.peerId;
        byte[] handshake;
        handshake = Utils.concat(Utils.concat(Utils.concat(Utils.concat(length_protocol, "BitTorrent protocol".getBytes()), reserve), this.infoHash), my_id);
        
        try {
			this.peer.getOutput().write(handshake);			
		} catch (IOException e) {
			System.out.println("IOException: "+e);			
		} catch (Exception e) {
			System.out.println("Exception: "+e);
		}
    }

}
